package com.example.service;


import com.example.entity.TUser;
import com.example.service.impl.TUserServiceImpl;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


@Component
public class MyUserService implements UserDetailsService {
    @Resource
    private TUserServiceImpl service;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        System.out.println(username);
        User user=null;
        int b=0;
        for (TUser a : service.queryall()) {
            System.out.println(a.getUsername());
            if (!username.equals(a.getUsername())) {
                b=1;
            } else {
                List<SimpleGrantedAuthority> authorities = new ArrayList<>();
                System.out.println(authorities);
                user = new User(username, new BCryptPasswordEncoder().encode(a.getPassword()), authorities);
            }
        }
        if (b==1){
            System.out.println("用户不存在");
        }
        return user;
    }
}



